﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Controllers
{
    public class OpusController : Controller
    {
        private readonly Manager.BLL.Admin.M_OpusBll bll = new Manager.BLL.Admin.M_OpusBll();
        //
        // GET: /Admin/Opus/

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.StylistSet, Ope = "view")]
        public ActionResult Index()
        {
            Manager.BLL.Admin.M_StylistBll bllStylist = new Manager.BLL.Admin.M_StylistBll();

            var query = from o in bll.GetModelList("")
                        select new Models.Opus
                        {
                            OpusID = o.OpusID,
                            OpusName = o.OpusName,
                            OpusDesc = o.OpusDesc,
                            OpusStylistName = bllStylist.GetModel(o.OpusStylistID.Value).StylistName,
                            OpusStylistDate = o.OpusStylistDate.Value,
                            IsTop = o.IsTop ? "是" : "否",
                            IsValid = o.IsValid ? "是" : "否"
                        };
            return View(query.ToList<Models.Opus>());
        }

        [HttpPost]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.OpusSet, Ope = "view")]
        public ActionResult Index(FormCollection forms)
        {
            try
            {
                Manager.BLL.Admin.M_StylistBll bllStylist = new Manager.BLL.Admin.M_StylistBll();

                string tbxSearchOpusID = forms["tbxSearchOpusID"];
                string tbxSearchOpusName = forms["tbxSearchOpusName"];
                string tbxSearchOpusTypeName = forms["tbxSearchOpusTypeName"];
                string tbxSearchIsValid = forms["tbxSearchIsValid"];
                string tbxSearchIsTop = forms["tbxSearchIsTop"];
                string tbxSearchOpusBeginDate = forms["tbxSearchOpusBeginDate"];
                string tbxSearchOpusEndDate = forms["tbxSearchOpusEndDate"];

                var query = from o in bll.GetModelList("")
                            select new Models.Opus
                            {
                                OpusID = o.OpusID,
                                OpusName = o.OpusName,
                                OpusDesc = o.OpusDesc,
                                OpusStylistName = bllStylist.GetModel(o.OpusStylistID.Value).StylistName,
                                OpusStylistDate = o.OpusStylistDate.Value,
                                IsTop = o.IsTop ? "是" : "否",
                                IsValid = o.IsValid ? "是" : "否"
                            };
                if (tbxSearchOpusID.Length > 0)
                    query = query.Where(s => s.OpusID == int.Parse(tbxSearchOpusID));
                if (tbxSearchOpusName.Length > 0)
                    query = query.Where(s => s.OpusName.Contains(tbxSearchOpusName));
                if (tbxSearchOpusTypeName.Length > 0)
                    query = query.Where(s => s.OpusStylistName.Contains(tbxSearchOpusTypeName));
                if (tbxSearchIsValid.Length > 0)
                    query = query.Where(s => s.IsValid.Equals(tbxSearchIsValid));
                if (tbxSearchOpusBeginDate.Length > 0)
                    query = query.Where(a => a.OpusStylistDate >= DateTime.Parse(tbxSearchOpusBeginDate));
                if (tbxSearchOpusEndDate.Length > 0)
                    query = query.Where(a => a.OpusStylistDate <= DateTime.Parse(tbxSearchOpusEndDate));
                if (tbxSearchIsTop.Length > 0)
                    query = query.Where(s => s.IsTop.Equals(tbxSearchIsTop));

                return View(query.ToList<Models.Opus>());
            }
            catch
            {
                Manager.BLL.Admin.M_StylistBll bllStylist = new Manager.BLL.Admin.M_StylistBll();

                var query = from o in bll.GetModelList("")
                            select new Models.Opus
                            {
                                OpusID = o.OpusID,
                                OpusName = o.OpusName,
                                OpusDesc = o.OpusDesc,
                                OpusStylistName = bllStylist.GetModel(o.OpusStylistID.Value).StylistName,
                                OpusStylistDate = o.OpusStylistDate.Value,
                                IsTop = o.IsTop ? "是" : "否",
                                IsValid = o.IsValid ? "是" : "否"
                            };
                return View(query.ToList<Models.Opus>());
            }
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.OpusSet, Ope = "add")]
        public ActionResult Add()
        {
            BindSelect();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.OpusSet, Ope = "add")]
        public ActionResult Add(Manager.Model.Admin.M_Opus model, FormCollection forms)
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

            try
            {
                if (!categoryBll.Exists(model.OpusStylistID.Value))
                    ModelState.AddModelError("", Resources.Global.msg_select_exist);

                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length != 0)
                    {
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "Opus");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("Opus", urlImage);
                        }
                        else
                        {
                            model.OpusImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    int r = bll.Add(model);
                    if (r > 0)
                        return RedirectToAction("Index", "Opus");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        /// <summary>
        /// 门店类别下拉列表框
        /// </summary>
        public void BindSelect()
        {
            Manager.BLL.Admin.M_StylistBll stylistBll = new Manager.BLL.Admin.M_StylistBll();
            IEnumerable<SelectListItem> stylists = from c in stylistBll.GetModelList("") select new SelectListItem { Value = c.StylistID.ToString(), Text = c.StylistName };
            ViewBag.StylistSelect = stylists;
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.OpusSet, Ope = "edit")]
        public ActionResult Edit(int id)
        {
            if (!bll.Exists(id))
                return RedirectToAction("Index");
            BindSelect();
            return View(bll.GetModel(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "edit")]
        public ActionResult Edit(int id, Manager.Model.Admin.M_Opus model, FormCollection forms)
        {
            Manager.BLL.Admin.M_CategoryBll stylistBll = new Manager.BLL.Admin.M_CategoryBll();
            model.OpusID = id;
            if (!bll.Exists(id))
                ModelState.AddModelError("", Resources.Global.msg_select_exist);
            else
                model.OpusImage = bll.GetModel(id).OpusImage;
            try
            {
                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length != 0)
                    {
                        bool res = (new Manager.Tools.FileUpload()).DeleteExistFile(Server.MapPath("~" + model.OpusImage));
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "Opus");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("OpusImage", urlImage);
                        }
                        else
                        {
                            model.OpusImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    if (!stylistBll.Exists(model.OpusStylistID.Value))
                        ModelState.AddModelError("", Resources.Global.msg_select_exist);
                    model.OpusDesc = forms["OpusDesc"];
                    bool r = bll.Update(model);
                    if (r)
                        return RedirectToAction("Index", "Opus");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.OpusSet, Ope = "del")]
        public ActionResult Del(int id)
        {
            if (!bll.Exists(id))
                return Content("<script>alert('" + Resources.Global.msg_select_exist + "');location.href='/Admin/Opus/Index';</script>");
            try
            {
                bool r = bll.Delete(id);
                if (r)
                    return Content("<script>alert('" + Resources.Global.msg_success + "');location.href='/Admin/Opus/Index';</script>");
                else
                    return Content("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Opus/Index';</script>");

            }
            catch (Exception)
            {
                return JavaScript("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Opus/Index';</script>");
            }
        }
    }
}
